import java.util.ArrayList;

import javax.xml.bind.JAXBElement.GlobalScope;

import processing.core.PConstants;

public class InfoPanel extends Component{

	// Scrollbar of the panel
	ScrollBar scrollBar;

	// Artist,Person,Group, buttons
	ButtonPlusMinus artist;
	ButtonPlusMinus person;
	ButtonPlusMinus group;

	int iconWidth =40;

	public InfoPanel(){
		scrollBar = new ScrollBar();

		artist = new ButtonPlusMinus("Artists");
		person = new ButtonPlusMinus("Persons");
		group = new ButtonPlusMinus("Groups");

	}




	public void draw(){

		// If not in graph mode don't show the info panel
		if(GLOBAL.analysisType != 2) return;

		drawBackGround();
		deactivateButtons();

		int linesCount=countLines();
		int totSize = GLOBAL.artistsSelected.size()+GLOBAL.personsSelected.size()+GLOBAL.groupsSelected.size();

		float j;
		if(((linesCount)*25+28)>height){
			scrollBar.width = 15;
			scrollBar.x = GLOBAL.width-15;
			scrollBar.y = y;
			scrollBar.height = GLOBAL.processing.height -55;
			scrollBar.draw();  
			scrollBar.size = 1/(float)(totSize);
			j = Main.map(scrollBar.val,0,1,0,totSize);
		}
		else j=0;


		int startPersons= GLOBAL.artistsSelected.size();
		int startGroups= GLOBAL.artistsSelected.size()+GLOBAL.personsSelected.size();


		int tot=0;
		int i=(int)(j+0.5);
		if(i==0){
			artist.y = y+tot*24 +6;
			artist.x = x+ 5;
			artist.active = true;
			artist.draw();
			GLOBAL.processing.fill(GLOBAL.textBoldColor);
			GLOBAL.processing.text("  ("+GLOBAL.artistsInfoButtonSelected.size()+")",artist.x+
					GLOBAL.processing.textWidth(artist.label)+20,artist.y+15);
		}
		tot++;
		// Draw artist info
		if(!artist.pluState){
			for( i=(int)(j+0.5); i<startPersons && (((tot)*24+30)<height);i++){			



				GLOBAL.artistsInfoButtonSelected.get(i).y = y+tot*24+6;
				GLOBAL.artistsInfoButtonSelected.get(i).x = x +10;
				GLOBAL.artistsInfoButtonSelected.get(i).active = true;
				GLOBAL.artistsInfoButtonSelected.get(i).draw();
				if(GLOBAL.artistsInfoButtonSelected.get(i).artist.additionalInfo.icon!=null){
					GLOBAL.processing.strokeWeight(2);
					GLOBAL.processing.stroke(80);
					GLOBAL.processing.fill(GLOBAL.colorBackground);
					GLOBAL.processing.rect(x+155, y +tot*24+6-12 , iconWidth, iconWidth);
					GLOBAL.processing.image(GLOBAL.artistsInfoButtonSelected.get(i).artist.additionalInfo.icon, x+155, y + tot*24+6-12,iconWidth,iconWidth);
				}
				tot+=2;
				if(!GLOBAL.artistsInfoButtonSelected.get(i).pluState)
					tot = drawArtistInfo(GLOBAL.artistsInfoButtonSelected.get(i),tot);


			}
		}
		if(i<startGroups ||i==0){
			person.y = y+tot*24 +6;
			person.x = x+ 5;
			person.active = true;
			person.draw();
			GLOBAL.processing.fill(GLOBAL.textBoldColor);
			GLOBAL.processing.text("  ("+GLOBAL.personsInfoButtonSelected.size()+")",person.x+
					GLOBAL.processing.textWidth(person.label)+20,person.y+15);
		}
		tot++;
		// Draw person info
		if(!person.pluState){
			int v=0;
			if(i-startPersons>=0)v =i-startPersons;
			for(int z=v; z<GLOBAL.personsInfoButtonSelected.size() && z<startGroups && (((tot)*24+30)<height);i++,z++){			

				GLOBAL.personsInfoButtonSelected.get(z).y = y+tot*24+6;
				GLOBAL.personsInfoButtonSelected.get(z).x = x +10;
				GLOBAL.personsInfoButtonSelected.get(z).active = true;
				GLOBAL.personsInfoButtonSelected.get(z).draw();
				if(GLOBAL.personsInfoButtonSelected.get(z).artist.additionalInfo.icon!=null){
					GLOBAL.processing.strokeWeight(2);
					GLOBAL.processing.stroke(80);
					GLOBAL.processing.fill(GLOBAL.colorBackground);
					GLOBAL.processing.rect(x+155, y +tot*24+6-12 , iconWidth, iconWidth);
					GLOBAL.processing.image(GLOBAL.personsInfoButtonSelected.get(z).artist.additionalInfo.icon, x+155, y + tot*24+6-12,iconWidth,iconWidth);
				}
				tot+=2;
				if(!GLOBAL.personsInfoButtonSelected.get(z).pluState)
					tot = drawArtistInfo(GLOBAL.personsInfoButtonSelected.get(z),tot);

			}
		}
		if(i<=totSize){
			group.y = y+tot*24 +6;
			group.x = x+ 5;
			group.active = true;
			group.draw();
			GLOBAL.processing.fill(GLOBAL.textBoldColor);
			GLOBAL.processing.text("  ("+GLOBAL.groupsInfoButtonSelected.size()+")",group.x+
					GLOBAL.processing.textWidth(group.label)+20,group.y+15);
		}
		tot++;

		// Draw group info
		if(!group.pluState){
			int v=0;
			if(i-startGroups>=0)v =i-startGroups;
			for(int z=v; z<GLOBAL.groupsInfoButtonSelected.size() && i<totSize && (((tot)*24+30)<height);i++, z++){			


				GLOBAL.groupsInfoButtonSelected.get(z).y = y+tot*24+6;
				GLOBAL.groupsInfoButtonSelected.get(z).x = x +10;
				GLOBAL.groupsInfoButtonSelected.get(z).active = true;
				GLOBAL.groupsInfoButtonSelected.get(z).draw();
				if(GLOBAL.groupsInfoButtonSelected.get(z).artist.additionalInfo.icon!=null){
					GLOBAL.processing.strokeWeight(2);
					GLOBAL.processing.stroke(80);
					GLOBAL.processing.fill(GLOBAL.colorBackground);
					GLOBAL.processing.rect(x+155, y +tot*24+6-12 , iconWidth, iconWidth);
					GLOBAL.processing.image(GLOBAL.groupsInfoButtonSelected.get(z).artist.additionalInfo.icon, x+155, y + tot*24+6-12,iconWidth,iconWidth);
				}
				tot+=2;
				if(!GLOBAL.groupsInfoButtonSelected.get(z).pluState)
					tot = drawArtistInfo(GLOBAL.groupsInfoButtonSelected.get(z),tot);


			}
		}

		// Draw loading function if a thread is loading data
		
		if(GLOBAL.artistsThreadsActive>0)
			GLOBAL.angleArtist = GLOBAL.loadingCircle(GLOBAL.selectionPanel.textBoxArtist.x-20, GLOBAL.selectionPanel.textBoxArtist.y+15, GLOBAL.angleArtist);
		
		
		removeButtons();
	}


	private void removeButtons(){

		for(int i=0;i<GLOBAL.artistsInfoButtonSelected.size();i++){
			if(GLOBAL.artistsInfoButtonSelected.get(i).cb.clicked){
				GLOBAL.artistAddRemove(GLOBAL.artistsInfoButtonSelected.get(i).artist);

			}
		}
		for(int i=0;i<GLOBAL.personsInfoButtonSelected.size();i++){
			if(GLOBAL.personsInfoButtonSelected.get(i).cb.clicked){
				GLOBAL.artistAddRemove(GLOBAL.personsInfoButtonSelected.get(i).artist);

			}
		}
		for(int i=0;i<GLOBAL.groupsInfoButtonSelected.size();i++){
			if(GLOBAL.groupsInfoButtonSelected.get(i).cb.clicked){
				GLOBAL.artistAddRemove(GLOBAL.groupsInfoButtonSelected.get(i).artist);

			}
		}
	}




	private int drawArtistInfo(ButtonPlusMinusArtist artistButton,int tot) {

		String beginDateString,endDateString,countryString,userString;
		int widthThreshold=200;

		GLOBAL.processing.textAlign(PConstants.LEFT);
		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.fill(GLOBAL.textColor);

		// Draw beginDate
		if(artistButton.artist instanceof Group)
			beginDateString = "Founding date: ";
		else if(artistButton.artist instanceof Person)
			beginDateString = "Birth date: ";
		else beginDateString = "Begin date: ";
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.text(beginDateString,x+25,y+tot*24+12);
		float width = GLOBAL.processing.textWidth(beginDateString);
		GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		GLOBAL.processing.text(artistButton.artist.additionalInfo.begin,x+25+width,y+tot*24+12);
		tot++;

		// Draw endDate
		if(artistButton.artist instanceof Group)
			endDateString = "Dissolution date: ";
		else if(artistButton.artist instanceof Person)
			endDateString = "Death date: ";
		else endDateString = "End date: ";
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.text(endDateString,x+25,y+tot*24+12);
		width = GLOBAL.processing.textWidth(endDateString);
		GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		GLOBAL.processing.text(artistButton.artist.additionalInfo.end,x+25+width,y+tot*24+12);
		tot++;

		// Draw Country and if Person Gender
		countryString = "Country: ";
		int countryStringWidth = (int)(GLOBAL.processing.textWidth(countryString+artistButton.artist.additionalInfo.country)+0.5);
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		width = GLOBAL.processing.textWidth(countryString);
		GLOBAL.processing.text(countryString,x+25,y+tot*24+12);
		GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		GLOBAL.processing.text(artistButton.artist.additionalInfo.country,x+25+width,y+tot*24+12);
		if(artistButton.artist instanceof Person){
			String sex = "Sex: ";
			GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
			GLOBAL.processing.text(sex,x+25+countryStringWidth+25,y+tot*24+12);
			width = GLOBAL.processing.textWidth(sex);
			GLOBAL.processing.textFont(GLOBAL.newFont, 14);
			GLOBAL.processing.text(artistButton.artist.additionalInfo.gender.toUpperCase().charAt(0),x+25+countryStringWidth+25+width,y+tot*24+12);
		}
		tot++;


		// Draw Aliases

		String[] aliases = artistButton.artist.additionalInfo.aliases;
		String aliasString = "Aliases: ";
		tot = drawAliasOrTags(aliases,aliasString,tot,widthThreshold,25);


		// Draw Tags

		//		String[] tags = artistButton.artist.additionalInfo.tags;
		//		String tagsString = "Tags: ";
		//		tot = drawAliasOrTags(tags,tagsString,tot,widthThreshold,25);


		// Draw Typical User who listen to this artist
		userString = "BreakDown of users: ";			
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		width = GLOBAL.processing.textWidth(userString);
		GLOBAL.processing.text(userString,x+25,y+tot*24+12);
		GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		tot++;

		// Draw most common user ages interval
		String userAge = "Ages: "; 

		String[] userAgeStrings = convertStringArrayListIntoArray(artistButton.artist.user_ages_intervals); 
		tot = drawAliasOrTags(userAgeStrings,userAge,tot,widthThreshold-8,33);

		// Draw most common user sex		
		String userSex = "Sex: "; 
		String userSexString=artistButton.artist.user_sex; 

		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		width = GLOBAL.processing.textWidth(userSex);
		GLOBAL.processing.text(userSex,x+33,y+tot*24+12);
		GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		GLOBAL.processing.text(userSexString,x+33+width,y+tot*24+12);
		tot++;

		// Draw most common user nationality
		String userNationality = "Nationalities: ";
		String[] userNationalityStrings = convertStringArrayListIntoArray(artistButton.artist.user_nationalities);  	
		tot = drawAliasOrTags(userNationalityStrings,userNationality,tot,widthThreshold-8,33);	
		return tot;

	}



	private String[] convertStringArrayListIntoArray(
			ArrayList<String> stringArrayList) {
		String[] strings = new String[stringArrayList.size()];

		for(int i=0;i< stringArrayList.size();i++){
			strings[i]=stringArrayList.get(i);
		}
		return strings;
	}




	// Function to Draw list of strings in the info panel
	private int drawAliasOrTags(String[] strings, String firstString, int tot, int widthThreshold,int widthStep){

		String writeString = firstString;
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		int lineWidth=(int)(GLOBAL.processing.textWidth(writeString)+0.5);
		if(strings == null) return tot;
		GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		for(int i=0;i<strings.length;i++){
			int tagsWidth = (int)(GLOBAL.processing.textWidth(strings[i])+0.5);
			if(lineWidth+tagsWidth < widthThreshold){
				lineWidth+=tagsWidth;
				if(i!=0)
					writeString = writeString +", "+ strings[i];
				else
					writeString = writeString +" "+ strings[i];
			}
			else{
				if(writeString.startsWith(firstString)){
					GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
					GLOBAL.processing.text(firstString,x+widthStep,y+tot*24+12);
					float width = GLOBAL.processing.textWidth(firstString);
					GLOBAL.processing.textFont(GLOBAL.newFont, 14);
					GLOBAL.processing.text(writeString.substring(firstString.length())+",",x+widthStep+width,y+tot*24+12);

				}
				else{GLOBAL.processing.textFont(GLOBAL.newFont, 14);
				GLOBAL.processing.text(writeString+",",x+widthStep,y+tot*24+12);
				}
				tot++;
				lineWidth=tagsWidth;
				writeString = strings[i];
			}
		}

		if(writeString.startsWith(firstString)){
			GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
			GLOBAL.processing.text(firstString,x+widthStep,y+tot*24+12);
			float width = GLOBAL.processing.textWidth(firstString);
			GLOBAL.processing.textFont(GLOBAL.newFont, 14);
			GLOBAL.processing.text(writeString.substring(firstString.length())+".",x+widthStep+width,y+tot*24+12);

		}
		else{GLOBAL.processing.textFont(GLOBAL.newFont, 14);
		GLOBAL.processing.text(writeString+".",x+widthStep,y+tot*24+12);
		}


		tot++;
		return tot;
	}


	private int countLines() {

		// Approximate count lines

		int linesCount =0;

		linesCount += (artist.pluState ? 1 : (GLOBAL.artistsSelected.size()*2+1));		
		linesCount += (person.pluState ? 1 : (GLOBAL.personsSelected.size()*2+1));
		linesCount += (group.pluState ? 1 : (GLOBAL.groupsSelected.size()*2+1));
		float lines=0;
		if(!artist.pluState)
			for(int i=0;i<GLOBAL.artistsInfoButtonSelected.size();i++){
				if(!GLOBAL.artistsInfoButtonSelected.get(i).pluState){
					lines += 4;
					lines += (float)(GLOBAL.artistsInfoButtonSelected.get(i).artist.additionalInfo.aliases.length)/3;
					//lines += (float)(GLOBAL.artistsInfoButtonSelected.get(i).artist.additionalInfo.tags.length)/3;'
					// Common user lines
					lines+=5;
				}
			}
		if(!person.pluState)
			for(int i=0;i<GLOBAL.personsInfoButtonSelected.size();i++){
				if(!GLOBAL.personsInfoButtonSelected.get(i).pluState){
					lines += 4;
					lines += (float)(GLOBAL.personsInfoButtonSelected.get(i).artist.additionalInfo.aliases.length)/3;
					//lines += (float)(GLOBAL.personsInfoButtonSelected.get(i).artist.additionalInfo.tags.length)/3;
					// Common user lines
					lines+=5;
				}
			}
		if(!group.pluState)
			for(int i=0;i<GLOBAL.groupsInfoButtonSelected.size();i++){
				if(!GLOBAL.groupsInfoButtonSelected.get(i).pluState){
					lines += 4;
					lines += (float)(GLOBAL.groupsInfoButtonSelected.get(i).artist.additionalInfo.aliases.length)/3;
					//lines += (float)(GLOBAL.groupsInfoButtonSelected.get(i).artist.additionalInfo.tags.length)/3;
					// Common user lines
					lines+=5;
				}
			}

		linesCount += (int) (lines+0.5);

		return linesCount;
	}




	private void drawBackGround() {
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.rectMode(PConstants.CORNERS);
		GLOBAL.processing.fill(GLOBAL.menuColor);
		GLOBAL.processing.rect(x, y, x+width, y+height);
	}

	private void deactivateButtons() {

		artist.active = false;
		person.active = false;
		group.active = false;
		for(int i=0;i<GLOBAL.groupsInfoButtonSelected.size();i++){
			GLOBAL.groupsInfoButtonSelected.get(i).active = false;
			GLOBAL.groupsInfoButtonSelected.get(i).cb.active = false;
		}

		for(int i=0;i<GLOBAL.personsInfoButtonSelected.size();i++){
			GLOBAL.personsInfoButtonSelected.get(i).active = false;
			GLOBAL.personsInfoButtonSelected.get(i).cb.active = false;

		}
		for(int i=0;i<GLOBAL.artistsInfoButtonSelected.size();i++){
			GLOBAL.artistsInfoButtonSelected.get(i).active = false;
			GLOBAL.artistsInfoButtonSelected.get(i).cb.active = false;

		}

	}

	public void mousePressed() {
		if(scrollBar.mouseOver())scrollBar.mousePressed();

		else if(artist.mouseOver() && artist.active ) artist.mouseCliked();
		else if(person.mouseOver() && person.active) person.mouseCliked();
		else if(group.mouseOver() && group.active) group.mouseCliked();

		else {
			boolean end=false;
			for(int i=0;i<GLOBAL.groupsInfoButtonSelected.size() && !end;i++){
				if(GLOBAL.groupsInfoButtonSelected.get(i).mouseOver() && GLOBAL.groupsInfoButtonSelected.get(i).active){
					GLOBAL.groupsInfoButtonSelected.get(i).mouseCliked();
					end = true; break;
				}
				else if(GLOBAL.groupsInfoButtonSelected.get(i).cb.mouseOver() && GLOBAL.groupsInfoButtonSelected.get(i).cb.active){
					GLOBAL.groupsInfoButtonSelected.get(i).cb.mouseCliked();
					end = true; break;
				}
				else if (GLOBAL.groupsInfoButtonSelected.get(i).mouseOverName() 
						&& GLOBAL.groupsInfoButtonSelected.get(i).active){
					GLOBAL.groupsInfoButtonSelected.get(i).nameClicked();
					end = true; break;
				}
			}
			for(int i=0;i<GLOBAL.personsInfoButtonSelected.size() && !end;i++){
				if(GLOBAL.personsInfoButtonSelected.get(i).mouseOver() && GLOBAL.personsInfoButtonSelected.get(i).active){
					GLOBAL.personsInfoButtonSelected.get(i).mouseCliked();
					end = true; break;
				}
				else if(GLOBAL.personsInfoButtonSelected.get(i).cb.mouseOver() && GLOBAL.personsInfoButtonSelected.get(i).cb.active){
					GLOBAL.personsInfoButtonSelected.get(i).cb.mouseCliked();
					end = true; break;
				}
				else if (GLOBAL.personsInfoButtonSelected.get(i).mouseOverName() 
						&& GLOBAL.personsInfoButtonSelected.get(i).active){
					GLOBAL.personsInfoButtonSelected.get(i).nameClicked();
					end = true; break;
				}
			}
			for(int i=0;i<GLOBAL.artistsInfoButtonSelected.size() && !end;i++){
				if(GLOBAL.artistsInfoButtonSelected.get(i).mouseOver()  && GLOBAL.artistsInfoButtonSelected.get(i).active){
					GLOBAL.artistsInfoButtonSelected.get(i).mouseCliked();
					end =true; break;
				}
				else if(GLOBAL.artistsInfoButtonSelected.get(i).cb.mouseOver()  && GLOBAL.artistsInfoButtonSelected.get(i).cb.active){
					GLOBAL.artistsInfoButtonSelected.get(i).cb.mouseCliked();
					end =true; break;
				}
				else if (GLOBAL.artistsInfoButtonSelected.get(i).mouseOverName() 
						&& GLOBAL.artistsInfoButtonSelected.get(i).active){
					GLOBAL.artistsInfoButtonSelected.get(i).nameClicked();
					end = true; break;
				}
			}
		}

	}


	public void mouseReleased() {
		scrollBar.mouseReleased();
	}

}
